<?php
namespace app\admin\validate;

use Think\Db;
use think\Validate;

class Admin extends Validate
{
    protected $rule = [
        'admin_username' => 'require|alphaNum|checkName:',
        'group_id' => 'require',
        'admin_pwd' => 'require|length:6,15',
        'admin_email' => 'require|email',
    ];
    protected $message = [
        'admin_username.require' => '用户名必填',
        'admin_username.alphaNum' => '用户名不合法',
        'group_id' => '分组必选',
        'admin_email.require' => '邮箱必填',
        'admin_email' => '邮箱格式错误',
        'admin_pwd' => '密码长度不合法',

    ];
    protected $scene = [
        'add' => ['admin_username', 'admin_email', 'admin_pwd', 'group_id'],
        'edit' => ['admin_email', 'group_id', 'admin_username' => 'require|alphaNum'],
        'pass' => ['admin_pwd'],
    ];

    // 自定义验证规则
    protected function checkName($value)
    {
        if (Db::name('Admin')->where('admin_username', $value)->find()) {
            return '用户名已存在';
        } else {
            return true;
        }
    }
}